package com.heima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.bean.Category;
import com.heima.common.PageParam;
import com.heima.common.R;
import com.heima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 增加分类
     * @param category
     * @return
     */
    @PostMapping
    public R add(@RequestBody Category category){
        int row = categoryService.add(category);
        if(row > 0){
            return R.success("添加分类成功！");
        }
        return R.error("添加分类失败！");
    }

    /**
     * 分页展示
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R page(PageParam pageParam){
        IPage<Category> page = categoryService.page(pageParam);
        return R.success(page);
    }

    /**
     * 刪除分类
     * @param category
     * @return
     */
    @DeleteMapping
    public R delete(Category category){
        System.out.println("category = " + category);
        int row = categoryService.delete(category);
        if(row > 0){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R update(@RequestBody Category category){
        int row = categoryService.update(category);
        if(row > 0){
            return R.success("修改分类成功！");
        }
        return R.error("修改分类失败！");
    }

    /**
     * 根据类型查找分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R findByType(Integer type){ //把 int 类型改成 Integer 类型，这样就能接收到为 null 的数据
        List<Category> list = categoryService.findByType(type);
        return R.success(list);
    }
}
